const db = require('../../models/index');
const Commodity = db.commoditys;
exports.CreateCommodity = (obj) => {

    return Commodity.create(obj)
}

// 查询所有数据
exports.findAll = (req, res) => {
    Commodity.findAll().then((data) => {
        res.send({
            data: data,
            code: 101,
            msg: "查询成功"
        })
    }).catch(err => {
        console.log(err.message + "查询数据出错")

    })
}

// 通过id删除数据
exports.delete = (req, res) => {

    Commodity.destroy({
        where: {
            id: req.body.id
        }
    }).then(data => {
        res.send({
            data: data,
            message: "删除成功",
            code: 102
        })
    }).catch(err => {
        res.send({
            data: null,
            message: "删除失败",
            code: 202
        })
    })
}

// 通过id更新数据
exports.update = (req, res) => {
    Commodity.update({
        title: req.body.title,
        price: req.body.price,
        detail: req.body.detail,
        type: req.body.type
    }, {
        where: {
            id: req.body.id
        }
    }).then(data => {
        if (data[0] !== 0) {
            res.send({
                data: data,
                message: "更新成功",
                code: 101
            })
        } else {
            res.send({
                data: null,
                message: "更新失败",
                code: 202
            })
        }
    }).catch(err => {
        console.log(err)
    })
}